<template>
  <div class="role">
    <PageForm
      :pageFormConfig="pageFormConfig"
      @resetBtnClick="resetBtnClick"
      @searchBtnClick="searchBtnClick"
    />
    <PageTable
      :pageTableConfig="pageTableConfig"
      pageName="role"
      title="角色"
      ref="pageTableRef"
      @newBtnClick="newBtnClick"
      @editBtnClick="editBtnClick"
    />
    <PageModel
      :creatOrEdit="creatOrEdit"
      :defaultInfo="defaultInfo"
      :otherInfo="otherInfo"
      ref="pageModelRef"
      pageName="role"
      :modelConfig="pageModelConfig"
      title="角色"
    >
      <div class="menu-tree">
        <el-tree
          ref="elTreeRef"
          :data="menus"
          show-checkbox
          node-key="id"
          :props="{ children: 'children', label: 'name' }"
          @check="handleCheckChange"
        >
        </el-tree>
      </div>
    </PageModel>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, nextTick, computed } from 'vue'
import PageForm from '@/components/PageForm/PageForm.vue'
import PageTable from '@/components/PageTable/PageTable.vue'
import { pageFormConfig } from './pageFormConfig'
import { pageTableConfig } from './pageTableConfig'
import { usePageSearch } from '@/hooks/usePageSearch'
import { usePageModel } from '@/hooks/usePageModel'
import { pageModelConfig } from './pageModelConfig'
import PageModel from '@/components/PageModel/PageModel.vue'
import { useStore } from '@/store'
import { ElTree } from 'element-plus'
import { menuMapLeafKeys } from '@/utils/mapMenusRouter'
export default defineComponent({
  components: {
    PageForm,
    PageTable,
    PageModel
  },
  setup() {
    const elTreeRef = ref<InstanceType<typeof ElTree>>()
    const store = useStore()
    const menus = computed(() => store.state.entireMenu)
    const otherInfo = ref({})
    const editCallback = (item: any) => {
      const leafKeys = menuMapLeafKeys(item.menuList)
      nextTick(() => {
        console.log(elTreeRef.value)
        elTreeRef.value?.setCheckedKeys(leafKeys, false)
      })
    }
    const handleCheckChange = (data1: any, data2: any) => {
      const checkedKeys = data2.checkedKeys
      const halfCheckedKeys = data2.halfCheckedKeys
      const menuList = [...checkedKeys, ...halfCheckedKeys]
      otherInfo.value = { menuList }
    }
    const [pageTableRef, resetBtnClick, searchBtnClick] =
      usePageSearch(pageFormConfig)
    const [pageModelRef, defaultInfo, newBtnClick, editBtnClick, creatOrEdit] =
      usePageModel(undefined, editCallback)

    return {
      creatOrEdit,
      pageModelConfig,
      otherInfo,
      handleCheckChange,
      menus,
      newBtnClick,
      editBtnClick,
      defaultInfo,
      pageTableRef,
      pageModelRef,
      elTreeRef,
      editCallback,
      pageFormConfig,
      pageTableConfig,
      resetBtnClick,
      searchBtnClick
    }
  }
})
</script>

<style scoped></style>
